package com.sc.study.controller;

import com.sc.study.dto.PlanQueryDTO;
import com.sc.study.entity.StudyPlan;
import com.sc.study.service.StudyPlanService;
import com.sc.study.vo.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * 学习计划控制器
 *
 * @author 徐赫阳
 * @date 2021/12/29
 */
@Api(description = "学习计划controller")
@RestController
@RequestMapping("/plan")
public class StudyPlanController {
    /**
     * 服务对象
     */
    @Autowired
    private StudyPlanService studyPlanService;

    @ApiOperation("分页获取计划")
    @PostMapping("/getPlanList")
    public PageVO<StudyPlan> getPlanList(@RequestBody PlanQueryDTO queryDTO){
        return studyPlanService.getPlanList(queryDTO);
    }

    @ApiOperation("保存")
    @PostMapping("/savePlan")
    public Integer savePlan(@RequestBody StudyPlan sysPlan, HttpServletRequest request) throws Exception{
        if(sysPlan.getId() == null){
            sysPlan.setCreatorId((Integer)request.getSession(true).getAttribute("user"));
        }else{
            sysPlan.setUpdaterId((Integer)request.getSession(true).getAttribute("user"));
        }
        studyPlanService.saveOrUpdate(sysPlan);
        return sysPlan.getId();
    }

    @ApiOperation("删除")
    @DeleteMapping(value = "/deletePlan/{id}")
    public void deletePlan(@PathVariable Integer id){
        studyPlanService.removeById(id);
    }



}
